package com.haizhi.crm.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.haizhi.crm.common.exception.BaseException;
import lombok.Data;

/**
 * Created by chengmo on 2018/5/2.
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageResponse extends Response {

    private long total;
    private int pageNo = 1;
    private int pageSize = 10;

    public PageResponse() {
    }

    public PageResponse(long total, int pageNo, int pageSize) {
        this.total = total;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public PageResponse(Object data, long total, int pageNo, int pageSize) {
        this(total, pageNo, pageSize);
        this.payload = data;
    }

    public long getTotalPages() {
        return (total + pageSize - 1) / pageSize;
    }

    public static PageResponse success() {
        return new PageResponse();
    }

    public static PageResponse error() {
        PageResponse response = new PageResponse();
        response.setSuccess(false);
        return response;
    }

    public static PageResponse error(BaseException ex) {
        PageResponse response = new PageResponse();
        response.setSuccess(false);
        response.setMessage(buildMessage(ex));
        return response;
    }

    public PageResponse setPageInfo(int pageTotal, int pageNo, int pageSize) {
        this.total = pageTotal;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        return this;
    }
}
